#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/11/6 17:45
# @Author  : supuchun
# @Site    :
# @File    : get_message.py
import json
import logging
import multiprocessing
import requests
from retrying import retry

from get_task import update_run_times
from model.mns_demo import ali_mns
from model.simlog import Logger

get_surf_failure_log = Logger('./log/get_surf_failure_log.log', logging.WARNING, logging.DEBUG)

class get_surf:
	def __init__(self):
		self.send_queue = "elephant-test-one"
		self.insert_queue = "elephant-test-two"
		self.vcg = "http://172.16.237.70/compare/api/getsimilarimages?result_size=1&img_url="
		self.quanjing = "http://172.16.237.70/compare/api/getsimilarimages?engine=quanjing&result_size=1&img_url="
		self.veer = "http://172.16.237.70/compare/api/getsimilarimages?engine=veer&result_size=1&img_url="
	
	# 选择相应的接口并请求数据
	def chose_api(self, compare_type=0, surf_url=''):
		if compare_type == 0:
			vcg_api_url = self.vcg + surf_url
			print vcg_api_url
			return json.loads(self.send_http(vcg_api_url))
		elif compare_type == 1:
			vcg_api_url = self.quanjing + surf_url
			return json.loads(self.send_http(vcg_api_url))
		elif compare_type == 2:
			vcg_api_url = self.veer + surf_url
			return json.loads(self.send_http(vcg_api_url))
	
	# 发送get http 请求

	def send_http(self, url='http://www.bejson.com/'):
		rs = requests.get(url).text
		return rs
	
	# 获取要查的表信息
	def get_table_info(self, compare_type=0):
		table_info = [{"table_name": "elephant_v2.ele_task", "suffix": ""},
		              {"table_name": "elephant_v2.ele_task_panorama", "suffix": "_panorama"},
		              {"table_name": "elephant_v2.ele_task_veer", "suffix": "_veer"}]
		return table_info[compare_type]
	
	def get_info(self):
		q = ali_mns(self.send_queue, self.insert_queue)
		rs_info = q.get_message(self.send_queue)
		rs = json.loads(rs_info, encoding="utf-8")
		surf_info = self.chose_api(rs['compare_type'], rs['surf_url'])
		tabname = 'compare_' + str(rs['company_id'])
		compare_id = rs['compare_id']
		company_id = rs['company_id']
		task_id = rs['task_id']
		# print surf_info
		if surf_info['code'] >= 500:
			
			if  rs.has_key('num'):
				rs['num'] += 1
			else:
				rs['num'] = 1
			if rs['num'] > 3:
				return False
				
			data = json.dumps(rs)
			print data
			rs_re = q.send_data(data)
			get_surf_failure_log.info(data)
			
			update_run_times(tabname, 11, compare_id)
			return False
		
		rs['result_id'] = surf_info['data'][0]['id']
		rs['from'] = surf_info['data'][0]['from']
		rs['surf_score'] = surf_info['data'][0]['surf_score']
		rs['n2_score'] = surf_info['data'][0]['n2_score']
		q_in = ali_mns(self.insert_queue, self.insert_queue)
		# print rs['from']
		rs_data = json.dumps(rs)
		res = q_in.send_data(rs_data)

def run():
	while True:
		a = get_surf()
		s = a.get_info()

if __name__ == '__main__':
	# pool = multiprocessing.Pool(processes=5)
	# for i in range(6):
	#
	# 	pool.apply_async(run, )
	#
	# pool.close()
	# pool.join()


	# #
	while True:
		a = get_surf()
		s = a.get_info()
